<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="./js/socket.io.min.js"></script>
</head>
<body>

<div>发送消息:</div>
<input type="text" id="msgContent"/>
<input type="button" value="点我发送" onclick="CHAT.chat()"/>

<div>接受消息：</div>
<div id="receiveMsg" style="background-color: gainsboro;"></div>

<script type="application/javascript">

    window.CHAT = {
        socket: null,
        init: function() {
            if (window.WebSocket) {
                // 测试wss 协议
                CHAT.socket = new WebSocket("wss://localhost:9887/websocket",["authorization","13124324"]);
                CHAT.socket = new WebSocket("ws://localhost:9001/websocket",["authorization","13124324"]);
                CHAT.socket.onopen = function() {
                    console.log("连接建立成功...");
                },
                    CHAT.socket.onclose = function(e) {
                        console.log("连接关闭...",e);
                    },
                    CHAT.socket.onerror = function(e) {

                        console.log("发生错误...",   e);
                    },
                    CHAT.socket.onmessage = function(e) {
                        console.log("接受到消息：" + e.data);
                        var receiveMsg = document.getElementById("receiveMsg");
                        var html = receiveMsg.innerHTML;
                        receiveMsg.innerHTML = html + "<br/>" + e.data;
                    }
            } else {
                alert("浏览器不支持websocket协议...");
            }
        },
        chat: function() {
            var msg = document.getElementById("msgContent");
            CHAT.socket.send(msg.value);
        }
    };

    CHAT.init();


</script>
</body>
</html>

